package hclfns

import (
	"github.com/zclconf/go-cty/cty/function"
)

type FunctionOptions struct {
	Stack           string
	StackSourceDir  string
	StackSourcesDir []string

	ModuleName string
	ModuleDir  string

	WorkingDir          string
	TFWrapperConfigPath string
	ConfigFilePath      string
	TerraformVersion    string
	TFWrapperVersion    string
}

// TerraformCommandsNeedInit List of terraform commands that need init
var TerraformCommandsNeedInit = []string{
	"apply", "refresh", "destroy", "plan", "output", "state",
}

// TerraformCommandsNeedLocking List of terraform commands that accept -lock-timeout
var TerraformCommandsNeedLocking = []string{
	"apply", "destroy", "import", "plan", "refresh", "taint", "untaint",
}

// TerraformCommandsNeedVars List of terraform commands that accept -var or -var-file
var TerraformCommandsNeedVars = []string{
	"apply", "console", "destroy", "import", "plan", "push", "refresh",
}

// TerraformCommandsNeedInput List of terraform commands that accept -input=
var TerraformCommandsNeedInput = []string{
	"apply", "import", "init", "plan", "refresh",
}

// TerraformCommandsNeedParallelism List of terraform commands that accept -parallelism=
var TerraformCommandsNeedParallelism = []string{
	"apply", "plan", "destroy",
}

// TerraformCommandsNeedStdOutput List of terraform commands that need stdout output
var TerraformCommandsNeedStdOutput = []string{
	"apply", "plan", "destroy", "output", "refresh", "state",
}

func WrapperFunctions(opts *FunctionOptions) map[string]function.Function {
	functions := map[string]function.Function{}
	//terraform wrapper function

	functions["get_repo_root"] = wrapVoidToStringAsFuncImpl(getRepoRoot, opts)
	functions["get_env"] = wrapStringSliceToStringAsFuncImpl(getEnv, opts)
	functions["get_stack"] = wrapVoidToStringAsFuncImpl(getStack, opts)
	functions["run_cmd"] = wrapStringSliceToStringAsFuncImpl(runCommand, opts)

	functions["get_module_dir"] = wrapStaticValueToStringAsFuncImpl(opts.ModuleDir)
	functions["get_module_name"] = wrapStaticValueToStringAsFuncImpl(opts.ModuleName)

	functions["get_stack_current_dir"] = wrapStaticValueToStringAsFuncImpl(opts.StackSourceDir)
	functions["get_stack_dirs"] = wrapStaticValueToStringSliceAsFuncImpl(opts.StackSourcesDir)

	functions["get_tfwrapper_dir"] = wrapVoidToStringAsFuncImpl(getTFWrapperDir, opts)
	functions["get_tfwrapper_config_name"] = wrapVoidToStringAsFuncImpl(getTFWrapperConfigName, opts)
	functions["get_tfwrapper_config_path"] = wrapVoidToStringAsFuncImpl(getTFWrapperConfigFile, opts)

	functions["get_terraform_version"] = wrapVoidToStringAsFuncImpl(getTerraformVersion, opts)
	functions["get_tfwrapper_version"] = wrapVoidToStringAsFuncImpl(getTFWrapperVersion, opts)

	functions["get_terraform_commands_that_need_vars"] = wrapStaticValueToStringSliceAsFuncImpl(TerraformCommandsNeedVars)
	functions["get_terraform_commands_that_need_input"] = wrapStaticValueToStringSliceAsFuncImpl(TerraformCommandsNeedInput)
	functions["get_terraform_commands_that_need_parallelism"] = wrapStaticValueToStringSliceAsFuncImpl(TerraformCommandsNeedParallelism)
	functions["get_terraform_commands_that_need_locking"] = wrapStaticValueToStringSliceAsFuncImpl(TerraformCommandsNeedLocking)
	return functions
}
